package com.csair.common.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import android.preference.PreferenceManager;
import android.util.DisplayMetrics;

import com.csair.common.CommonApplication;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class LanguageUtil {
    private static final String LOCALE_LANGUAGE = "Locale_Language";
    private static List<Locale> mSupport;
    private static Locale mDefault = Locale.CHINA;

    /**
     * 在 App 第一次启动时读取以前版本的语言配置，若不存在，则使用当前系统语言配置
     */
    public static void initLocaleAtTheFirstBoot(Context context) {
        String language = getLocalLanguage(context);
        Locale locale = new Locale(language, Locale.getDefault().getCountry());

        refreshLanguage(locale);
    }

    /**
     * 判断当前语言环境是否为中文
     */
    public static boolean isChinese() {
        return !isEnglish();
    }

    /**
     * 判断当前语言环境是否为英文
     */
    public static boolean isEnglish() {
        return Locale.US.getLanguage().equals(getLocalLanguage(CommonApplication.getApplication()));
    }

    /**
     * 更新 Application 或 Activity 的Context
     */
    public static Context refreshContext(Context context) {
        String language = getLocalLanguage(context);
        Locale locale = new Locale(language, Locale.getDefault().getCountry());

        Configuration overrideConfiguration = context.getResources().getConfiguration();
        overrideConfiguration.setLocale(locale);
        if (Build.VERSION.SDK_INT > 23)
            overrideConfiguration.setLocales(new LocaleList(locale));
        return context.createConfigurationContext(overrideConfiguration);
    }

    /**
     * 切换语言，暂时只是中英文切换
     */
    public static void changeLanguage() {
        setLanguage(isChinese() ? Locale.US : Locale.CHINA);
    }

    public static Locale getLanguage() {
        Context context = CommonApplication.getApplication();
        return new Locale(getLocalLanguage(context), Locale.getDefault().getCountry());
    }

    /**
     * 设置语言
     */
    public static void setLanguage(Locale locale) {
        locale = new Locale(locale.getLanguage(), Locale.getDefault().getCountry());
        saveLocalLanguage(locale.getLanguage());
        refreshLanguage(locale);
    }

    /**
     * 将用户设置的语言保存在本地
     */
    public static void saveLocalLanguage(String language) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(CommonApplication.getApplication());
        sp.edit().putString(LOCALE_LANGUAGE, language).apply();
    }

    /**
     * 获取本地设置的语言，本地设置语言为空，则取默认语言
     */
    public static String getLocalLanguage(Context context) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        return sp.getString(LOCALE_LANGUAGE, getDefaultLocale().getLanguage());
    }

    /**
     * 更新语言
     */
    public static void refreshLanguage(Locale locale) {
        Locale.setDefault(locale);

        Resources resources = CommonApplication.getApplication().getResources();
        Configuration configuration = resources.getConfiguration();
        configuration.locale = locale;
        configuration.setLocale(locale);
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        resources.updateConfiguration(configuration, displayMetrics);
    }

    /**
     * 获取系统默认语言
     * 如果系统语言不在APP支持的语言列表内，则返回默认语言
     */
    public static Locale getDefaultLocale() {
        if (mSupport == null) {
            mSupport = new ArrayList<>();
            mSupport.add(Locale.CHINA);
            mSupport.add(Locale.US);
        }

        Locale locale = Locale.getDefault();
        boolean include = false;
        for (Locale l : mSupport) {
            if (locale.getLanguage().equals(l.getLanguage())) {
                include = true;
            }
        }

        if (!include) locale = new Locale(mDefault.getLanguage(), locale.getCountry());
        return locale;
    }

    /**
     * 设置APP支持的语言环境
     */
    public static void setSupport(List<Locale> support) {
        mSupport = support;
    }

    /**
     * 设置默认语言
     * 默认为中文
     */
    public static void setDefault(Locale locale) {
        mDefault = locale;
    }
}
